package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.base.response.JsonResult;
import cn.tedu._05weibo.base.response.StatusCode;
import cn.tedu._05weibo.base.utils.CacheUtil;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.List;

@Validated
@Api(tags = "2.微博模块")
@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
//    @Autowired
//    private RedisTemplate<String, Serializable> redisTemplate;
    @Autowired
    private CacheUtil cacheUtil;
    @ApiOperation("发送微博")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO,@ApiIgnore HttpSession session){
        UserVO user = (UserVO) session.getAttribute("user");
//        if(user==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }改用拦截器进行校验
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setUserId(user.getId());
        weiboMapper.insert(weibo);
        return JsonResult.ok();
    }
    @ApiOperation("微博首页")
    @GetMapping("selectIndex")
    public JsonResult selectIndex(){
        /*设计:redis字符串缓存{"index_cache":"weiboIndexVOS"}
        * 1.从缓存中(redis)获取首页数据:WeiboIndexVO
        *   1.1若缓存区存在数据则直接返回
        *   1.2若缓存中不存在数据
        *       则从MySQL数据库中查找数据
        *       将数据保存至redis中,数据:WeiboIndexVO
        *       返回数据*/
        //Serializable indexCache = redisTemplate.opsForValue().get("index_cache");
        Serializable indexCache = cacheUtil.get("index_cache");
        if (indexCache!=null){
            System.out.println("数据来自缓存Redis");
            return JsonResult.ok(indexCache);
        }
        List<WeiboIndexVO> weiboList= weiboMapper.selectIndex();
        //redisTemplate.opsForValue().set("index_cache", (Serializable) weiboList);
        cacheUtil.setExpire("index_cache", (Serializable) weiboList,30);
        System.out.println("数据来自MySQL");
        return JsonResult.ok(weiboList);
    }
    @ApiOperation("查询微博详情")
    @GetMapping("selectById")
    @ApiImplicitParam(name="id",value = "微博id",required = true, dataType = "long")
    public JsonResult selectById(@Range(min=200,max=300,message = "微博编号必须在200-300之间") @RequestParam Long id){
        if (id<0){//手动抛出异常,测试全局异常处理
            throw new IllegalArgumentException("微博id不可小于0");
        }
        WeiboDetailVO weiboDetailVOList =weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVOList);
    }
}
